﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Alogient.Cameleon.Sdk;
using Alogient.Cameleon.Sdk.Items;

namespace Alogient.Cameleon.Sdk.tests
{
    [TestFixture]
    public class CMPageTest : FixtureBase
    {
        [SetUp]
        public void Init()
        {
        }

        [TearDown]
        public void Finish()
        {
        }

        [Test]
        public void CreatePage1()
        {
            var navId = this.navID;
            var page = new CMPage(navId);

            Assert.AreEqual(navId, page.NavID);
        }

        [Test]
        public void CreatePage2()
        {
            var navId = this.navID;
            var cultureCode = frCultureCode;

            var page = new CMPage(navId, cultureCode);

            Assert.AreEqual(navId, page.NavID);
            Assert.AreEqual(cultureCode, page.CultureCode);
        }
        
        protected CMPage LoadPage(int navID, string cultureCode)
        {
            return new CMPage(navID, cultureCode);
        }

        [Test]
        public void GetParent()
        {
            var navId = this.navID;
            var cultureCode = frCultureCode;
            var page = this.LoadPage(navId, cultureCode);

            var parentPage = page.Parent;

            Assert.AreEqual(0, parentPage.NavID);
        }

        [Test]
        public void GetBreadcrumbs()
        {
            var navId = this.navID;
            var cultureCode = frCultureCode;
            var page = this.LoadPage(navId, cultureCode);

            var breadcrumb = page.Breadcrumbs;

            Assert.AreEqual(1, breadcrumb.Count);
        }

        [Test]
        public void GetSubpages()
        {
            var navId = 0;
            var cultureCode = frCultureCode;
            var page = this.LoadPage(navId, cultureCode);

            var subpages = page.Subpages;

            Assert.AreEqual(1, subpages.Count);
        }

        [Test]
        public void GetCMSubpages()
        {
            var navId = 0;
            var cultureCode = frCultureCode;
            var page = this.LoadPage(navId, cultureCode);

            var subpages = page.CMSubpages;

            Assert.AreEqual(0, subpages.Count);

            page.XmlDepth = 1;

            subpages = page.CMSubpages;

            Assert.AreEqual(1, subpages.Count);
        }

        [Test]
        public void GetPageTitle()
        {
            var navId = this.navID;
            var cultureCode = frCultureCode;
            var page = this.LoadPage(navId, cultureCode);

            var title = page.GetPageTitle();

            Assert.AreEqual(page.PageTitle, title);
        }

        [Test]
        public void GetElement()
        {
            var navId = this.navID;
            var cultureCode = frCultureCode;
            var page = this.LoadPage(navId, cultureCode);

            var elements = page.Elements;

            Assert.AreEqual(4, elements.Count);
        }

        [Test]
        public void GetMedia()
        {
            var navId = this.navID;
            var cultureCode = frCultureCode;
            var page = this.LoadPage(navId, cultureCode);

            var media = page.GetMedia(mediaPartName);

            var element = new CMElement(navID, cultureCode, mediaPartName);
            var path = string.Format("{0}/Media/{1}_{2}_{3}_0_{4}", CMSettings.AdminUrl, this.navID, element.ElementID, element.CultureCode, element.File);
            
            Assert.AreEqual(path, media);
        }

        [Test]
        public void GetImage()
        {
            var navId = this.navID;
            var cultureCode = frCultureCode;
            var page = this.LoadPage(navId, cultureCode);

            var image = page.GetImage(imagePartName);
            var element = new CMElement(this.navID, frCultureCode, imagePartName);

            var pathOriginal = string.Format("{0}/Media/{1}_{2}_{3}_0_{4}", CMSettings.AdminUrl, this.navID, element.ElementID, element.CultureCode, element.File);
            var pathPresentation = string.Format("{0}/Media/{1}_{2}_{3}_1_{4}", CMSettings.AdminUrl, this.navID, element.ElementID, element.CultureCode, element.File);
            var pathThumbnail = string.Format("{0}/Media/{1}_{2}_{3}_2_{4}", CMSettings.AdminUrl, this.navID, element.ElementID, element.CultureCode, element.File);


            Assert.AreEqual(pathOriginal, image);

            image = page.GetImage(imagePartName, ImageFileType.Original);

            Assert.AreEqual(pathOriginal, image);

            image = page.GetImage(imagePartName, ImageFileType.Presentation);

            Assert.AreEqual(pathPresentation, image);

            image = page.GetImage(imagePartName, ImageFileType.Thumbnail);

            Assert.AreEqual(pathThumbnail, image);
        }

        [Test]
        public void GetAssociation()
        {
            var navId = this.navID;
            var cultureCode = frCultureCode;
            var page = this.LoadPage(navId, cultureCode);

            var association = page.GetAssociation(associationPartName);

            Assert.AreEqual(page.NavID, association.NavID);
        }

        [Test]
        public void GetMultiAssociation()
        {
            var navId = this.navID;
            var cultureCode = frCultureCode;
            var page = this.LoadPage(navId, cultureCode);

            var multiAssociation = page.GetMultiAssociation(associationPartName);

            Assert.AreEqual(1, multiAssociation.Count);
        }

        [Test]
        public void GetImageWidth()
        {
            var navId = this.navID;
            var cultureCode = frCultureCode;
            var page = this.LoadPage(navId, cultureCode);

            var width = page.GetImageWidth(imagePartName);

            Assert.AreEqual(0, width);

            width = page.GetImageWidth(imagePartName, ImageFileType.Original);

            Assert.AreEqual(0, width);

            width = page.GetImageWidth(imagePartName, ImageFileType.Presentation);

            Assert.AreEqual(0, width);

            width = page.GetImageWidth(imagePartName, ImageFileType.Thumbnail);

            Assert.AreEqual(0, width);
        }

        [Test]
        public void GetImageHeight()
        {
            var navId = this.navID;
            var cultureCode = frCultureCode;
            var page = this.LoadPage(navId, cultureCode);
            
            var height = page.GetImageHeight(imagePartName);

            Assert.AreEqual(0, height);

            height = page.GetImageHeight(imagePartName, ImageFileType.Original);

            Assert.AreEqual(0, height);

            height = page.GetImageHeight(imagePartName, ImageFileType.Presentation);

            Assert.AreEqual(0, height);

            height = page.GetImageHeight(imagePartName, ImageFileType.Thumbnail);

            Assert.AreEqual(0, height);
        }
    }
}
